package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.CommentTab_1RecyclerViewAdapter;
import com.example.administrator.liveapp.adapter.CommentTab_2RecyclerViewAdapter;
import com.example.administrator.liveapp.adapter.CommentViewPagerAdapter;
import com.example.administrator.liveapp.adapter.ReservationTab_1RecyclerViewAdapter;
import com.example.administrator.liveapp.adapter.ReservationViewPagerAdapter;
import com.example.administrator.liveapp.bean.LiveOnlineBean;
import com.example.administrator.liveapp.bean.MyCommentBean;
import com.example.administrator.liveapp.bean.OfflineActivitiesBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;
import com.zhy.view.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的评论activity
 */

public class MyComment extends AppCompatActivity implements View.OnClickListener{

    private ViewPager comment_viewPager;
    private List<View> viewContainter2;
    private ViewPagerIndicator id_indicator;
    private List<String> mDatas;
    View comment_ViewPager_tab1;
    View comment_ViewPager_tab2;
    private RecyclerView comment_tab1_Recycle, comment_tab2_Recycle;
    private TextView no_content1, no_content2;
    private String consumerId;//用户Id
    private MyCommentBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_comment);
        Intent intent = getIntent();
        consumerId = intent.getStringExtra("consumerId");
        comment_viewPager = (ViewPager) findViewById(R.id.comment_viewpager);
        id_indicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
        findViewById(R.id.comment_returns).setOnClickListener(this);
        comment_ViewPager_tab1 = LayoutInflater.from(this).inflate(R.layout.comment_tab1_recyle, null);
        comment_ViewPager_tab2 = LayoutInflater.from(this).inflate(R.layout.comment_tab2_recyle, null);
        comment_tab1_Recycle = (RecyclerView) comment_ViewPager_tab1.findViewById(R.id.comment_tab1_Recycle);
        comment_tab2_Recycle = (RecyclerView) comment_ViewPager_tab2.findViewById(R.id.comment_tab2_Recycle);
        no_content1 = (TextView) comment_ViewPager_tab1.findViewById(R.id.no_content1);
        no_content2 = (TextView) comment_ViewPager_tab2.findViewById(R.id.no_content2);
        mDatas = new ArrayList<>();
        mDatas.add("视频");
        mDatas.add("文章");
        viewContainter2 = new ArrayList<>();
        viewContainter2.add(comment_ViewPager_tab1);
        viewContainter2.add(comment_ViewPager_tab2);
        CommentViewPagerAdapter reservationViewPagerAdapter = new CommentViewPagerAdapter(viewContainter2);
        id_indicator.setVisibleTabCount(2);//设置可见Tab数量
        id_indicator.setTabItemTitles(mDatas);//设置tab标题
        comment_viewPager.setAdapter(reservationViewPagerAdapter);
        id_indicator.setViewPager(comment_viewPager, 0);//绑定ViewPager
        initData();
    }

    private void initData() {
        String url = AppUrl.URL + AppUrl.ListCommentURL;
        Map<String, String> map = new HashMap<>();
        map.put("consumerId", consumerId);
        XUtils.Get(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result, MyCommentBean.class);
                try {
                    bean.getMyVideoComment().get(0).getCommentcontent().toString();
                    comment_tab1_Recycle.setVisibility(View.VISIBLE);
                    FullyLinearLayoutManager linerLayoutManager = new FullyLinearLayoutManager(MyComment.this);
                    linerLayoutManager.setScrollEnabled(true);
                    comment_tab1_Recycle.setHasFixedSize(true);
                    comment_tab1_Recycle.setLayoutManager(linerLayoutManager);
                    CommentTab_1RecyclerViewAdapter tab1 = new CommentTab_1RecyclerViewAdapter(bean.getMyVideoComment());
                    comment_tab1_Recycle.setAdapter(tab1);

                } catch (Exception e) {
                    comment_tab1_Recycle.setVisibility(View.GONE);
                    no_content1.setVisibility(View.VISIBLE);
                }

                try {
                    bean.getMyArticleComment().get(0).getArticleTitle().toString();
                    comment_tab2_Recycle.setVisibility(View.VISIBLE);
                    FullyLinearLayoutManager linerLayoutManager2 = new FullyLinearLayoutManager(MyComment.this);
                    linerLayoutManager2.setScrollEnabled(true);
                    comment_tab2_Recycle.setHasFixedSize(true);
                    comment_tab2_Recycle.setLayoutManager(linerLayoutManager2);
                    CommentTab_2RecyclerViewAdapter tab2 = new CommentTab_2RecyclerViewAdapter(bean.getMyArticleComment());
                    comment_tab2_Recycle.setAdapter(tab2);
                } catch (Exception e) {
                    comment_tab2_Recycle.setVisibility(View.GONE);
                    no_content2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                Toast.makeText(MyComment.this, ex.getMessage() + "", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.comment_returns:
                finish();
                break;
        }
    }
}
